using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MissionBeaconHolder : MonoBehaviour
{
	public static MissionBeaconHolder instance;

	public List<MissionBeacon> missionBeaconList = new List<MissionBeacon>();

	public MissionBeacon closesMissionBeacon;

	private int skippedAnimations;

	private float minDistanceToTony = 10f;

	public MissionBeacon prevClosesMissionBeacon;

	private Quaternion newTonyRotation;

	private void Awake()
	{
		instance = this;
		missionBeaconList.AddRange(GetComponentsInChildren<MissionBeacon>());
	}

	private void Start()
	{
		StartCoroutine(GetClosestBeacon());
		InGameTutorial.StartTutorial("new_mission_Tutorial", GameTopWindow.instance.gameObject.transform.parent, string.Empty);
	}

	private IEnumerator GetClosestBeacon()
	{
		while (true)
		{
			yield return new WaitForSeconds(1f);
			prevClosesMissionBeacon = closesMissionBeacon;
			minDistanceToTony = 20f;
			closesMissionBeacon = null;
			if (!settings.offlineMode || !(GameController.instance.playerScript != null))
			{
				continue;
			}
			foreach (MissionBeacon beacon in missionBeaconList)
			{
				float distance = Vector3.Distance(GameController.instance.playerScript.transform.position, beacon.transform.position);
				if (distance < minDistanceToTony)
				{
					minDistanceToTony = distance;
					closesMissionBeacon = beacon;
				}
			}
			if ((bool)closesMissionBeacon)
			{
				if ((closesMissionBeacon != prevClosesMissionBeacon || skippedAnimations >= 5) && closesMissionBeacon.tonyAnimator != null)
				{
					closesMissionBeacon.tonyAnimator.SetTrigger("Demo");
					skippedAnimations = 0;
				}
				else
				{
					skippedAnimations++;
				}
			}
		}
	}

	private void Update()
	{
		if (!(closesMissionBeacon == null) && closesMissionBeacon.gameObject.activeInHierarchy)
		{
			newTonyRotation = closesMissionBeacon.tonyAnimator.transform.rotation;
			newTonyRotation.SetLookRotation(GameController.instance.playerScript.transform.position - closesMissionBeacon.tonyAnimator.transform.position, Vector3.up);
			newTonyRotation.x = 0f;
			newTonyRotation.z = 0f;
			closesMissionBeacon.tonyAnimator.transform.rotation = Quaternion.Lerp(closesMissionBeacon.tonyAnimator.transform.rotation, newTonyRotation, Time.deltaTime * 5f);
		}
	}
}
